// pages/shopping/shopping.js
var {carlist,getphon,delecar} = require('../../utils/util');
Page({
  /**
   * 页面的初始数据
   */
  data: {

    array:[],
    price:0,
    length:1,
    img:'http://img.9lele.com',
    jiesuan:1,
    user:'',
    items: [],
    startX: 0, //开始坐标
    startY: 0
    
    
  },
  account(){
    wx.navigateTo({
      url: '/pages/account/account',
    })
  },
  // 获取手机号

    getPhoneNumber (e) {
      // console.log(e.detail.errMsg)
      // console.log(e.detail.iv)
      // console.log(e.detail.encryptedData)
      wx.setStorage({
        key:"phon",
        data:JSON.stringify(e.detail)
      })
  },

  
  show(){
    this.sumprice();
  },
  tapName: function(event) {
    // console.log(event.currentTarget.dataset.index)
    let i = event.currentTarget.dataset.index;
    this.data.array[i].checked =! this.data.array[i].checked
    this.sumprice();
  },
  checkall(){
    for(let i=0;i< this.data.array.length;i++){
      this.data.array[i].checked = true
    }
    this.sumprice();
   
    this.setData({
      array:this.data.array
    })
  },

  sumprice(){
    var sum=0;
    for(let i=0;i<this.data.array.length;i++){
      if(this.data.array[i].checked == true){
        sum = sum + this.data.array[i].price*1
      }
    }
    this.setData({
      price:sum
    })
    
  }
  ,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

for (var i = 0; i < 10; i++) {
this.data.items.push({
    content: i + " 向左滑动删除哦,向左滑动删除哦,向左滑动删除哦,向左滑动删除哦,向左滑动删除哦",
    isTouchMove: false //默认隐藏删除
    })
}

this.setData({
items: this.data.items
});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    this.setData({
      length:this.data.array.length
    })
    wx.getStorage({
      key: 'openid',
      success (res) {
        // console.log(res.data)
        let arr = JSON.parse(res.data)
        // console.log(arr)
        that.setData({
          user:arr
        })
              wx.request({
              url: carlist(), //仅为示例，并非真实的接口地址
              data: {
                member_id:arr.member_id
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success (res) {
                console.log(res.data.data)
                that.setData({
                  array:res.data.data
                })
                
              }
            })
      }
    })
    
    // 获取手机判断
    wx.getStorage({
      key: 'phon',
      success (res) {
        // console.log(res.data)
        let arr = JSON.parse(res.data)
        console.log(arr)
        
        let user = that.data.user
        console.log(128,user)
            wx.request({
            url: getphon(), //仅为示例，并非真实的接口地址
            data: {
              session_key:user.info.session_key,
              iv:arr.iv,
              encryptedData:arr.encryptedData,
              openid:user.info.openid

            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
              console.log(res.data)
            }
          })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 删除购物车列表数据
  delete(e){
    let that = this
    console.log(e.currentTarget.dataset.index)
    let id  =e.currentTarget.dataset.index
    wx.request({
      url: delecar(id), //仅为示例，并非真实的接口地址
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1000
        })
        that.onShow();
      }
    })

  },
})